package ru.mydesignstudio.contactmanager.web;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import ru.mydesignstudio.contactmanager.domain.Contact;
import ru.mydesignstudio.contactmanager.service.ContactService;

@Controller
public class ContactController {
	@Autowired
	private ContactService contactService;
	
	@RequestMapping("/index")
	public String listContacts(Map<String, Object> map) {
		map.put("Contact", new Contact());
		map.put("ContactList", this.contactService.listContact());
		
		return "Contact";
	}
	
	@RequestMapping("/")
	public String home() {
		return "redirect:/index";
	}
	
	@RequestMapping(value="/add", method=RequestMethod.POST)
	public String addContact(@ModelAttribute("contact") Contact contact,
            BindingResult result) {
		
		this.contactService.addContact(contact);
		return "redirect:/index";
	}
	
	@RequestMapping("/delete/{contactId}")
	public String removeContact(@PathVariable("contactId") Integer contactId) {
		this.contactService.removeContact(contactId);
		return "redirect:/index";
	}
}
